package com.atdc.utill;

import java.util.Arrays;

public class Cal {
    public static int FirstFit(int[] ta, int first, int second, int third, int fourth, int port) {
        int start, end;
        int nextstart = 0;
        t1:
        for (int i = 0; i < ta.length; i++) {

            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == first) {
                        Arrays.fill(ta, start, end + 1, port);
                        break t1;
                    }
                }

            }
        }
        t2:
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == second) {
                        Arrays.fill(ta, start, end + 1, port + 1);
                        break t2;
                    }
                }

            }
        }
        t3:
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == third) {
                        Arrays.fill(ta, start, end + 1, port + 2);
                        break t3;
                    }
                }

            }
        }
        t4:
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == fourth) {
                        Arrays.fill(ta, start, end + 1, port + 3);
                        nextstart = end + 1;
                        break t4;
                    }
                }
            }
        }
        return nextstart;
    }

    public static int NextFit(int[] ta, int first, int second, int third, int fourth, int port, int next) {
        int start, end;
        t1:
        for (int i = next; i < ta.length; i++) {

            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == first) {
                        Arrays.fill(ta, start, end + 1, port);
                        next = end + 1;
                        break t1;
                    }
                }

            }
        }
        t2:
        for (int i = next; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == second) {
                        Arrays.fill(ta, start, end + 1, port + 1);
                        next = end + 1;
                        break t2;
                    }
                }

            }
        }
        t3:
        for (int i = next; i < ta.length; i++) {

            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == third) {
                        Arrays.fill(ta, start, end + 1, port + 2);
                        next = end + 1;
                        break t3;
                    }
                }

            }
        }
        t4:
        for (int i = next; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = i;
                for (end = start; end < ta.length && ta[end] == 0; end++) {
                    if (end - start + 1 == fourth) {
                        Arrays.fill(ta, start, end + 1, port + 3);
                        next = end + 1;
                        break t4;
                    }
                }
            }
        }
        return next;
    }

    public static int BestFit(int[] ta, int first, int second, int third, int fourth, int portname, int nextstart) {
        int start, end, restart = 0, reend = 0;
        int minlen = Integer.MAX_VALUE;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= first && (end - start) < minlen) {
                    minlen = end - start;
                    restart = start;
                    reend = restart + first;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname);
        minlen = Integer.MAX_VALUE;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= second && (end - start) < minlen) {
                    minlen = end - start;
                    restart = start;
                    reend = restart + second;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname + 1);
        minlen = Integer.MAX_VALUE;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= third && (end - start) < minlen) {
                    minlen = end - start;
                    restart = start;
                    reend = restart + third;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname + 2);
        minlen = Integer.MAX_VALUE;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= fourth && (end - start) < minlen) {
                    minlen = end - start;
                    restart = start;
                    reend = restart + fourth;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname + 3);


        return reend;
    }

    public static int WorstFit(int[] ta, int first, int second, int third, int fourth, int portname, int nextstart) {
        int start, end, restart = 0, reend = 0;
        int maxlen = 0;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= first && (end - start) > maxlen) {
                    maxlen = end - start;
                    restart = start;
                    reend = restart + first;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname);
        maxlen = 0;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= second && (end - start) > maxlen) {
                    maxlen = end - start;
                    restart = start;
                    reend = restart + second;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname + 1);
        maxlen = 0;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= third && (end - start) > maxlen) {
                    maxlen = end - start;
                    restart = start;
                    reend = restart + third;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname + 2);
        maxlen = 0;
        for (int i = 0; i < ta.length; i++) {
            if (ta[i] == 0) {
                start = end = i;
                while (ta[i] == 0 && i != ta.length - 1) {
                    i++;
                    end++;
                }
                if (end - start >= fourth && (end - start) > maxlen) {
                    maxlen = end - start;
                    restart = start;
                    reend = restart + fourth;
                }
            }

        }
        Arrays.fill(ta, restart, reend, portname + 3);


        return reend;

    }

    public static int QuickFit(int[] size, int first, int second, int third, int fourth, int portname, int nextstart) {
        int reend = 0, height = size.length;
        int max = height;
        while (max >= first) {
            max /= 2;
        }
        max *= 2;
        for (int i = 0; i < size.length; i += max) {
            boolean flag = true;
            for (int j = i; j < i + first; j++) {
                if (size[i] != 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                Arrays.fill(size, i, i + first, portname);
                reend=i+first;
                break;
            }
        }
        height = size.length;
         max = height;
        while (max >= second) {
            max /= 2;
        }
        max *= 2;
        for (int i = 0; i < size.length; i += max) {
            boolean flag = true;
            for (int j = i; j < i + second; j++) {
                if (size[i] != 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                Arrays.fill(size, i, i + second, portname+1);
                reend=i+second;
                break;
            }
        }
        height = size.length;
        max = height;
        while (max >= third) {
            max /= 2;
        }
        max *= 2;
        for (int i = 0; i < size.length; i += max) {
            boolean flag = true;
            for (int j = i; j < i + third; j++) {
                if (size[i] != 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                reend=i+third;
                Arrays.fill(size, i, i + third, portname+2);

                break;
            }
        }
        height = size.length;
        max = height;
        while (max >= fourth) {
            max /= 2;
        }
        max *= 2;
        for (int i = 0; i < size.length; i += max) {
            boolean flag = true;
            for (int j = i; j < i + fourth; j++) {
                if (size[i] != 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                Arrays.fill(size, i, i + fourth, portname+3);
                reend=i+first;
                break;
            }
        }


        return reend;
    }
}
